package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.common.JWTUtil;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.domain.TUserInfo;
import com.woniu.car.exception.ParamException;
import com.woniu.car.mapper.TUserInfoMapper;
import com.woniu.car.parameter.AddUserInfoParam;
import com.woniu.car.parameter.UserInfoParam;
import com.woniu.car.service.TUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户详情 服务实现类
 * </p>
 *
 * @author zhoubinbin
 * @since 2021-06-21
 */
@Service
public class TUserInfoServiceImpl extends ServiceImpl<TUserInfoMapper, TUserInfo> implements TUserInfoService {
    @Resource
    private TUserInfoMapper userInfoMapper;

    //注册成功后的用户详情新增


    //用户详情更新
    @Override
    public ResponseEntity updateUserInfo(UserInfoParam userInfoParam, HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        if (token==null){
            return new ResponseEntity(500,"您当前未登陆,请登陆后进行操作",null);
        }
        if (userInfoParam.getGender()!=0&&userInfoParam.getGender()!=1&&userInfoParam.getGender()==null){
            throw new ParamException("性别参数0代表男，1代表女请传入正确的值");
        }
        String id = JWTUtil.getId(token);
        Integer userId = Integer.valueOf(id);
        QueryWrapper<TUserInfo> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id",userId);
        TUserInfo tUserInfo = userInfoMapper.selectOne(wrapper1);
        BeanUtils.copyProperties(userInfoParam,tUserInfo);
        UpdateWrapper<TUserInfo> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",userId);
        int update = userInfoMapper.update(tUserInfo, wrapper);
        if (update>0){
            return new ResponseEntity(400,"更新成功",tUserInfo);
        }else {
            return new ResponseEntity(400,"更新失败",tUserInfo);
        }

    }
    //查询用户详情
    @Override
    public ResponseEntity getUserInfo(Integer userId)  {
        QueryWrapper<TUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        TUserInfo tUserInfo = userInfoMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(tUserInfo)){
            return new ResponseEntity(400,"查询失败",tUserInfo);
        }else {
            return new ResponseEntity(200,"查询成功",tUserInfo);
        }
    }

    @Override
    public ResponseEntity updateTel(HttpServletRequest request, String tel) throws Exception {

        //校验前端输入的手机号格式是否正确
        String telq = "^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$";
        String token = request.getHeader("token");
        if (token==null){
            return new ResponseEntity(500,"您当前未登陆,请登陆后进行操作",null);
        }
        String id = JWTUtil.getId(token);
        Integer userId = Integer.valueOf(id);
        System.out.println(userId);
        if (tel.matches(telq)){
            TUserInfo tUserInfo = userInfoMapper.selectById(userId);
            tUserInfo.setTel(tel);
            int i = userInfoMapper.updateById(tUserInfo);
            if (i>0){
                return ResponseEntity.builder(TUserInfo.class).setCode(200).setMsg("修改成功");
            }else {
                return ResponseEntity.builder(TUserInfo.class).setCode(200).setMsg("修改失败");
            }
        }else{
            return new ResponseEntity().setCode(500).setMsg("请输入正确的中国大陆手机号码");
        }
    }

    @Override
    public ResponseEntity addUserInfo(AddUserInfoParam addUserInfoParam) {

        TUserInfo tUserInfo = new TUserInfo();
        tUserInfo.setIntegral(0);
        BeanUtils.copyProperties(addUserInfoParam,tUserInfo);
        int insert = userInfoMapper.insert(tUserInfo);
        TUserInfo tUserInfo1 = userInfoMapper.selectById(addUserInfoParam.getUserId());
        if (insert>0){
            return new ResponseEntity(200,"新增成功",tUserInfo1);
        }else {
            return new ResponseEntity(200,"新增失败",null);
        }
    }
}
